package br.com.rubrol.util;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;

import br.com.rubrol.model.Cidade;
import br.com.rubrol.model.Cliente;
import br.com.rubrol.model.Endereco;
import br.com.rubrol.model.Estado;
import br.com.rubrol.model.Fornecedor;
import br.com.rubrol.model.Maquina;
import br.com.rubrol.model.Produto;
import br.com.rubrol.model.Transportadora;
import br.com.rubrol.model.Usuario;

import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
	private JAXBContext context;
	private Class<?>[] types = { 
			Maquina.class,
			Usuario.class,
			Produto.class,
			Estado.class,
			Cidade.class,
			Endereco.class,
			Fornecedor.class,
			Transportadora.class,
			Cliente.class
	};

	public JAXBContextResolver() throws Exception {
		this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
				types);
	}

	public JAXBContext getContext(Class<?> objectType) {
		for (Class<?> type : types) {
			if (type == objectType) {
				return context;
			}
		}
		return null;
	}

}